describe("防疫-机器人管理", () => {
  beforeEach(() => {
    const username = "admin";
    const password = "123456";
    cy.login(username, password);

    cy.intercept("POST", "/api/robot-archives/v1/getCompanyRobotList").as(
      "getCompanyRobotList"
    );

    cy.visit("/disinfect/robot-manage");
  });

  it("进入机器人管理-页面显示", () => {
    const tableTh = ["编号", "机器人名称", "类型", "状态", "操作"];
    cy.get("table>thead>tr>th").should("have.length", tableTh.length);
    for (let i = 0; i < 5; i++) {
      cy.get("table>thead>tr>th").contains(tableTh[i]);
    }
    cy.get("table>tbody>tr").then(($tr) => {
      expect($tr.length).to.greaterThan(0);
    });
  });
  context("绑定机器人", () => {
    it("成功", () => {
      bindingRobot();
      cy.wait("@bindingRobot")
        .its("response.body")
        .then(($res) => {
          const title = $res.message;
          cy.contains(`${title}`);
        });
    });

    it("失败", () => {
      bindingRobot();
      cy.wait("@bindingRobot")
        .its("response.body")
        .then(($res) => {
          const title = $res.message;
          cy.contains(`${title}`);
          cy.contains("未知错误");
        });
    });
  });

  it("查询搜索", () => {
    cy.getByData("common-list")
      .find("input")
      .eq(0)
      .as("resettingInput")
      .type("46");
    cy.contains("重置查询").click();
    cy.get("@resettingInput").should("not.have.value", 46);
  });

  it("机器人绑定电梯-成功", () => {
    cy.getByData("itlcloud-binding").eq(0).click();
    cy.contains("绑定电梯机器人");
    // 拦截获取电梯机器人请求
    cy.intercept("GET", "/api/itlcloud-robot/v1/list*").as("getElevatorRobot");
    // 选择电梯配置
    cy.contains("label", "电梯配置")
      .siblings(".el-form-item__content")
      .find("[placeholder='请选择']")
      .click();
    cy.getByData("itlcloud-name").first().click();
    // 等待请求
    cy.wait("@getElevatorRobot")
      .its("response.body")
      .should(($res) => {
        expect($res.data.list.length).to.greaterThan(0);
      });
    // 选择电梯机器人
    cy.contains("label", "电梯机器人")
      .siblings(".el-form-item__content")
      .find("[placeholder='请选择']")
      .click();
    cy.getByData("itlcloud-robot-name").first().click();
    cy.intercept("POST", "/api/itlcloud-robot/v1/create").as("bindingElevator");
    cy.contains("确认").click();
    // 等待请求
    cy.wait("@bindingElevator");
    cy.contains("保存成功");
  });

  context("解绑机器人-", () => {
    it("成功", () => {
      deleteRobot();
      cy.wait("@deleteRobot");
      cy.contains("保存成功");
    });

    it.only("失败", () => {
      deleteRobot();
      cy.wait("@deleteRobot")
        .its("response.body")
        .then(($res) => {
          const title = $res.message;
          cy.contains(`${title}`);
          cy.contains("未知错误");
        });
    });
  });
});

function bindingRobot() {
  cy.contains("绑定机器人").click();
  cy.contains("label", "名称").siblings().find("input").type("测试1");
  cy.contains("label", "ID").siblings().find("input").type("06");
  cy.contains("label", "密码").siblings().find("input").type("123456");
  cy.contains("确定").click();
  cy.intercept("POST", "/api/robot-archives/v1/binding*").as("bindingRobot");
  cy.contains("警告").closest(".el-message-box").contains("确定").click();
}

function deleteRobot() {
  cy.wait("@getCompanyRobotList");
  cy.intercept("POST", "/api/robot-archives/v1/unBinding/*").as("deleteRobot");
  cy.contains("解绑").click();
}
